package com.lc.demo1.controller;

import com.lc.demo1.vo.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @author liuc
 * @date 2021/5/10
 * @description
 **/
@Controller
public class LoginController {

    @Value("${server.port}")
    private String tomcatPort;

    @RequestMapping("toLogin")
    public String toLogin() {
        System.out.println("去登录页面");
        return "login";
    }

    @RequestMapping("/result")
    public String success(){
        System.out.println("已经登录跳转至result!");
        return "result";
    }

    @RequestMapping("/login")
    public String login(User user, HttpSession httpSession){
        if(check(user)) {
            System.out.println("合法用户");
            String tag = "_come_from_demo1";
            httpSession.setAttribute("username",user.getUsername() + tag);
            return "redirect:/result";
        }else{
            System.out.println("未检测到session");
            return "redirect:/toLogin";
        }
    }

    private boolean check(User user){
        if(user == null){
            return false;
        }
        return "admin".equals(user.getUsername()) && "admin".equals(user.getPassword());
    }
}
